#include "ope.h"

#define DATABASE "my.db"

void *thread_fun(void *arg);

int main(int argc, char const *argv[])
{
    if(argc < 3){
        fprintf(stderr,"Usage:%s <ip> <port>\n",argv[0]);
        exit(1);
    }
    //数据库操作
    THREAD_PASS pass;
    if(sqlite3_open(DATABASE,&(pass.db)) != SQLITE_OK){
        printf("error: %s\n", sqlite3_errmsg(pass.db));
        exit(1);
    }
    printf("open DATABASE success\n");
    char *errmsg;
    if(sqlite3_exec(pass.db,"create table employee(job int primary key,name text,pwd text,type char,level int,age int,addr text,phone text,salary double)", NULL, NULL, &errmsg) != SQLITE_OK)
    {
        printf("error:%s\n",errmsg);
    }else{
        printf("success:the table is created successfuly\n");
    }
    //创建日志文件
    if((pass.fd = open("log.txt",O_RDWR|O_CREAT|O_APPEND,0666)) == -1)
    {
        ERRLOG("OPEN");
        return -1;
    }

    //创建套接字
    int sockfd;
    struct sockaddr_in serveraddr,clientaddr;
    socklen_t addrlen = sizeof(struct sockaddr_in);

     //第一步：创建套接字
    if((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
    {
        ERRLOG("socket error");
    }

    //第二步：填充服务器网络信息结构体
    //inet_addr：将点分十进制ip地址转化为无符号32位整形数据
    //htons：将主机字节序转化为网络字节序
    //atoi：将数字型字符串转化为整形数据   "1568" --> 1568
    serveraddr.sin_family = AF_INET;
    serveraddr.sin_addr.s_addr = inet_addr(argv[1]);
    serveraddr.sin_port = htons(atoi(argv[2]));

    //第三步：将套接字与服务器网络信息结构体绑定
    if(bind(sockfd, (struct sockaddr *)&serveraddr, addrlen) == -1)
    {
        ERRLOG("bind error");
    }

    //第四步：将套接字设置为被动监听状态
    if(listen(sockfd, 5) == -1)
    {
        ERRLOG("listen error");
    }
  
    while(1)
    {
        pass.accept_fd = accept(sockfd,(struct sockaddr *)&(pass.clientaddr),&addrlen);
        if(pass.accept_fd == -1){
            perror("accept");
        }
        printf("accept success\n");

        //输出客户端的信息
        pthread_t pthread;
        if(pthread_create(&pthread,NULL,thread_fun,(void*)&pass) != 0){
            ERRLOG("pthread_create error");
        
        }
    }
    return 0;
    
}

void *thread_fun(void *arg){
    pthread_detach(pthread_self());
    MSG msg;
    ssize_t recvbytes;
    THREAD_PASS pass = *(THREAD_PASS *)arg;
    
    while(1)
    {
        memset(&msg,0,sizeof(MSG));
        if((recvbytes = recv(pass.accept_fd,&msg,sizeof(MSG),0)) == -1){
            ERRLOG("recv error");
            return NULL;
        }
        
        if(recvbytes==0)
        {
            printf("客户端已经断开\n");
            pthread_exit(NULL);
        }
        
        printf("接收到%d\n",msg.Usemsg.job);
        switch (msg.ope)
        {
        case LOGIN:
            printf("登录\n");
            login_fun(arg,&msg);
            break;
        case INC:   //增加
            inc_fun(arg,&msg);
            break;
        case DEL:   //删除
            del_fun(arg,&msg);
            break;
                    //修改
        case REM_PWD:
            rem_pwd_fun(arg,&msg);
            break;
        case REM_ADDR:
            rem_addr_fun(arg,&msg);
            break;
        case REM_PHONE:
            rem_phone_fun(arg,&msg);
            break;
        case REM_NAME:
            rem_name_fun(arg,&msg);
        case REM_SASLARY:
            rem_saslary_fun(arg,&msg);
        case REM_AGE:
            rem_age_fun(arg,&msg);
        case REM_LEVEL:
            rem_level_fun(arg,&msg);
        case FIND:
            rem_find_fun(arg,&msg);
        case FINDALL:   //查找
            rem_findall_fun(arg,&msg);
        case HISTORY:
            rem_history_fun(arg,&msg);
        default:
            break;
        }
    }
}